package com.system.permissiondemo.filter;


import com.system.permissiondemo.common.RequestHolder;
import com.system.permissiondemo.entity.SysUser;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @Description:    登录拦截（校验用户是否登录）
* @Author:         Joe
* @CreateDate:     2020/3/27 11:51
*/
@Slf4j
public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        SysUser sysUser = (SysUser)request.getSession().getAttribute("user");
        if (sysUser == null){
            log.info("用户信息不存在，请先登录");
           //重定向到登录页面
            String path = "login.html";
            //response.sendRedirect(path);
            return;
        }
        RequestHolder.add(sysUser);
        RequestHolder.add(request);
        filterChain.doFilter(servletRequest,servletResponse);
        log.info("用户信息完整，且已经登录");
        return;

    }

    @Override
    public void destroy() {

    }

}
